<?php
			
	/* SVN FILE: $Id: view.php 103 2011-02-28 22:46:37Z andreoletti $ */
	
	/**
	 * Project Name : arcadia
	 *
	 * @author $Author: andreoletti $
	 * @version $Revision: 103 $
	 * @lastrevision $Date: 2011-02-28 23:46:37 +0100 (lun, 28 feb 2011) $
	 * @filesource $URL: http://svn.fletchlynd.com/arcadia/trunk/addressbook/view.php $
	 */

	require_once 'include/functions.php';
	require_once 'include/const.inc.php';
	require_once '../include/auth.inc.php';
	require_once '../include/conn.inc.php';

	require_once('../include/view.php');

	if (!empty($iView)) {
	  	$sHeaderTitle = $sAPP_NAME." &raquo; ".$aData['sLastname'].(!empty($aData['sFirstname']) ? " ".$aData['sFirstname'] : "").
	  			(isset($aData['sCompany']) && !empty($aData['sCompany']) ? " &raquo; ".$aData['sCompany'] : "").
	  			(isset($aData['sDepartment']) && !empty($aData['sDepartment']) ? " &raquo; ".$aData['sDepartment'] : "");
		$sTableTitle = "<div style='font-size: 0.7em'>".(isset($aData['sCompany']) && !empty($aData['sCompany']) ? $aData['sCompany'] : "")." ".(isset($aData['sDepartment']) && !empty($aData['sDepartment']) ? $aData['sDepartment'] : "")."</div><div>".$aData['sLastname']." ".$aData['sFirstname']."</div>";
	} else  {
		$sHeaderTitle = $sAPP_NAME." &raquo; $sTableTitle";
	}

	$html .= "<div id='tabs'><ul>$sTabsHtml</ul>";
	
	$i = 1;
	if (is_allowed('view', fnFieldAccessLvl($iAccessLvl, $i))) {
		
		$html .= "<div id='tabs-$i'>";
			
			$html .= ($sAction == 'edit' ? "<form method='post' id='form-tab-$i' class='buttons' action='transact.php'>" : '');
			$html .= "<table class='standard' border='".fnTableBorder()."' width='98%'>";
			
				$html .= "<col width='25%'><col width='25%'><col width='25%'><col width='25%'>";	
				$html .= "<tbody>";

				$html .= "<tr><td>".fnSelectInput('sPrefix', 'titolo', $aData, $aNAME_PREFIX)."</td><td colspan='3'>".fnTextInput('sJobtitle', 'job title', $aData, 0)."</td></tr>";
				$html .= "<tr><td>".fnTextInput('sLastname', 'cognome', $aData, 30)."</td><td>".fnTextInput('sFirstname', 'nome', $aData, 30)."</td>
					<td colspan='2'>".(!empty($iView)  && $aData['sCreatedby'] == fnReadSessionValue('sUsername') ? fnCheckbox('iNotShared', "contatto non condiviso", $aData, 1) : '&nbsp;')."</td></tr>";
				$html .= "<tr><td colspan='4'>".fnTextInput('sCompany', 'azienda', $aData, 40)."</td></tr>";
				$html .= "<tr><td colspan='4'>".fnTextInput('sDepartment', 'dipartimento/unità', $aData, 40)."</td></tr>";
				$html .= "<tr><td colspan='2'>".fnTextInput('sField_1', 'Tel 1', $aData, 40)."</td><td colspan='2'>&nbsp;</td></tr>";
				$html .= "<tr><td colspan='2'>".fnTextInput('sField_2', 'Tel 2', $aData, 40)."</td><td colspan='2'>&nbsp;</td></tr>";
				$html .= "<tr><td colspan='2'>".fnTextInput('sField_3', 'Email 1', $aData, 40)."</td><td colspan='2'>&nbsp;</td></tr>";
				$html .= "<tr><td colspan='2'>".fnTextInput('sField_4', 'Email 2', $aData, 40)."</td><td colspan='2'>&nbsp;</td></tr>";
					
				$html .= "<tr><td colspan='4'>&nbsp;</td></tr>"; 		
				
				if (empty($iView) || (!empty($iView) && $sAction != 'edit' && !empty($aData['sNote'])) || (!empty($iView) && $sAction == 'edit'))	
					$html .= "<tr><td colspan='3'>".fnTextareaInput('sNote', 'note', $aData, '70%', '10em', false)."</td><td>&nbsp;</td></tr>"; 		
			
				$html .= "</tbody>";
			$html .= "</table>";
			
			if ($iView != 0 && $sAction != 'edit') { 
				$html .= "<h5>".fnHtmlLastModify($aData['tsLastModify'])."</h5>";
				if (!empty($aData['sCreatedby'])) { 
					$html .= "<input type='hidden' name='sCreatedby' value='".$aData['sCreatedby']."'>";
				} else {
					$html .= "<input type='hidden' name='sCreatedby' value='".fnReadSessionValue('sUsername')."'>";
				}	
			} else {
				$html .= "<h5>&nbsp;</h5>";
				$html .= "<input type='hidden' name='sCreatedby' value='".fnReadSessionValue('sUsername')."'>";	
			}
			$html .= fnButtons($iView, $i, $iAccessLvl, $aData);
	
		$html .= "</div>";
	
	}
	
	$i = $iTabUploads;
	if (is_allowed('view', fnFieldAccessLvl($iAccessLvl, $sViewItemLink, $i))) { 
		
		$html .= "<div id='tabs-$i'>";			
			$html .= fnUploadsTabContent($iView, $sViewItemLink, $i);
		$html .= "</div>";
	
	}
		
	$html .= "</div>";
	
	$html .= "<div id='not-shared-dialog' title='Contatto non condiviso'>";
	$html .= "<p>Selezionando questa opzione il contatto sarà visibile solo dall'utente che ha creato la scheda.</p>";
	$html .= "</div>";
	
	// contenuto <head> della pagine
	$sHeadContent .= fnViewHeadContent($iTab, $aDisabledTabs);
	
	$bSEARCH_FORM_SHOW = false; // il form di ricerca non viene visualizzato nello script $sViewItemLink
								// non posso assegnare subito questo valore a $bSEARCH_FORM_SHOW perchè
								// alcune funzioni in ../include/view.php utilizzano questa variabile.
								
	require_once('../themes/'.$sTHEME.'/template.php');
	die();




	function fnViewHeadContent ($iTab, $aTabsDisabled) {

		global $sSUITE_NAME, $sAPP;
		
		global $iView, $sViewItemLink;
		
		global $sAction;
		
		global $sJsDocumentReady;
		
		global $aTabsLabels;
		
		$sTabsDisabled = '';
		foreach ($aTabsDisabled as $i) { $sTabsDisabled .= "$i, "; }
		$sTabsDisabled = ($sTabsDisabled!='' ? " disabled: [".substr_replace( $sTabsDisabled, "", -2 )."] " : '');

		$iTabSelected = $i = 0;
		foreach ($aTabsLabels as $sKey => $sValue) {
			if ($sKey == $iTab) { $iTabSelected = $i; }
			$i++;
		}
		
		$sSelectedTab = ($iTabSelected!=0 && !in_array($iTabSelected, $aTabsDisabled) ? ($sTabsDisabled!="" ? ", " : '') . " selected: $iTabSelected " : "");

		$sHeadContent = "
		
			<script type='text/javascript' charset='utf-8'>
	
				$().ready(function() {
					
					var oClicked = null;
					
					$sJsDocumentReady
					
					$('#tabs').tabs({ $sTabsDisabled $sSelectedTab });
				
					$('button, input:submit', '.buttons').button();
				
					$('#iNotShared').click(function() {
						oClicked = $(this);
						src = oClicked.attr('src'),
							onCheck = /\_on\.png$/,
							offCheck = /\_off\.png$/;
						if (oClicked.attr('src').match(offCheck)) {
							$('#not-shared-dialog').dialog('open');
						} else if ($(this).attr('src').match(onCheck)) { 
							fnToggle(oClicked);
						}
					});
					
					$('#not-shared-dialog').dialog( {
						autoOpen: false,
						resizable: false,
						modal: true,
						buttons: {
							Ok: function() {
								fnToggle(oClicked);
								$(this).dialog('close');
							},
							Annulla: function() {
								oClicked = null;
								$(this).dialog('close');
							}
						}
					});
					
					function fnToggle (oObj) {
						oCliked = null;
						src = oObj.attr('src'),
							onCheck = /\_on\.png$/,
							offCheck = /\_off\.png$/;
						$.getJSON('../include/toggle.php', {'sAction': 'toggle',
															'sField': oObj.attr('sField'),
															'sValue': (src.match(onCheck) ? 0 : 1),
															'iView': '$iView',
															'sTable': '',
															'sViewItemLink': '$sViewItemLink',
															'sAPP': '$sAPP'	}, function (json) {
							if (json[0].sAction=='toggle' && json[0].sError=='') { 
								if(src.match(onCheck)) {
									oObj.attr('src', src.replace(onCheck, '_off.png'));
								} else if (src.match(offCheck)) {
									oObj.attr('src', src.replace(offCheck, '_on.png'));
								}
							} else { 
								alert ('Error: ' + json[0].sError);
							} 		
						})
					}; " . ($sAction == 'edit' ? "
					
					$('#sCompany').autocomplete({
						source: './company_suggestions.php',
						minLength: 2,
						select: function(event, ui) {
							$('#sCompany').val(ui.item.value);
						}
					});
				
					$('#sDepartment').autocomplete({
						source: './department_suggestions.php',
						minLength: 2,
						select: function(event, ui) {
							$('#sDepartment').val(ui.item.value);
						}
					}); " : '') . "	
		    
				});
				
				</script> ";
	
		return $sHeadContent;
	}

?>